﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.Script.Services;
using System.Web.Services;
using Gencode.PrsWeb.Controls.GridControls;
using Gencode.Common.SQL;
using Gencode.PrsCommon.App;

namespace Gencode.PrsWeb
{
    public partial class Personalplaner : Gencode.Web.Common.WebPage//System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
			Gencode.PrsWeb.App.User info = Session["USERRIGHTS"] as Gencode.PrsWeb.App.User;
            if (info == null)
                Response.Redirect("Login.aspx");

            dienstModule.Controls.Add(LoadControl("Modules\\DienstView.ascx"));

			if (Request.QueryString["PLID"] != null)
			{
				planModule.Controls.Add(LoadControl("Modules\\PlanView.ascx"));
			}
			else
				_planTable.Style.Add("visibility", "hidden");

        }

		[ScriptMethod]
		[WebMethod]
		public static DienstForPlanGrid OnDienstCellClick(int prsid, int abteilungid, int day, int planid, int columnIndex, int rowIndex)
		{
			DienstForPlanGrid dfpg;
			int dienstID;

			//TODO:
			//should use GridArgs...
			Gencode.PrsWeb.App.User info = HttpContext.Current.Session["USERRIGHTS"] as Gencode.PrsWeb.App.User;
			if (info == null)
				HttpContext.Current.Response.Redirect("Login.aspx");

			if (HttpContext.Current.Session["DIENSTID"] != null && HttpContext.Current.Session["DIENSTID"].ToString() != string.Empty)
			{
				dienstID = int.Parse(HttpContext.Current.Session["DIENSTID"].ToString());
				dfpg = new DienstForPlanGrid(dienstID, info.Mandid, "R" + rowIndex + "C" + columnIndex);
			}
			else
			{
				dienstID = 0;
				dfpg = new DienstForPlanGrid("R" + rowIndex + "C" + columnIndex);
			}

			string datum = Gencode.PrsCommon.App.AppData.PlanDetails.GetPlanDateVon(planid);

			int y = int.Parse(DateUtil.GetYear(datum));
			int m = int.Parse(DateUtil.GetMonth(datum));

            Gencode.PrsCommon.Data.Dienst.DienstData.SaveDienstInPlan(planid,
                prsid, abteilungid, new DateTime(y, m, day).ToShortDateString(), dienstID);

            //Querry query = new Querry();
            //query.SetSTPName("set_updsetdienst");
            //query.AddParameter("@planid", planid);
            //query.AddParameter("@prsid", prsid);
            //query.AddParameter("@abteilungid", abteilungid);
            //query.AddParameter("@datum", DateUtil.ConvertDateToSQL(new DateTime(y,m,day).ToShortDateString()));
            //query.AddParameter("@dienstid", dienstID);
            //query.Execute();

			dfpg.RowIndex = rowIndex;

			DataView dv = Gencode.PrsCommon.App.AppData.DataGenerator.GetSaldo(prsid, abteilungid, planid);
			double soll = Util.RoundDouble(double.Parse(dv[0].Row["soll"].ToString()));
			double haben = Util.RoundDouble(double.Parse(dv[0].Row["haben"].ToString()));
			double vortrag = Util.RoundDouble(double.Parse(dv[0].Row["vortrag"].ToString()));

			dfpg.Vortrag = vortrag;
			dfpg.Soll = soll;
			dfpg.Haben = haben;
			dfpg.Diff = Util.RoundDouble(vortrag + (haben - soll));

			return dfpg;
		}
    }
}
